package com.yuanfengerp.purchase.service.impl;


import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.purchase.dao.IPurchaseReturnDao;
import com.yuanfengerp.purchase.entity.PurchaseReturn;
import com.yuanfengerp.purchase.service.IPurchaseReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseReturnServiceImpl implements IPurchaseReturnService {
    @Autowired
    private IPurchaseReturnDao iprd;

    @Override
    public Result<List<PurchaseReturn>> queryPurchaseReturn(Map<String, Object> params) {
        List<PurchaseReturn> purchaseReturns = iprd.queryPurchaseReturn(params);
        return Result.success(purchaseReturns);
    }

    @Override
    public Result<String> addPurchaseReturn(PurchaseReturn purchaseReturn) {
        purchaseReturn.setPurchase_return_audit_time(new Date());
        purchaseReturn.setPurchase_return_operation_time(new Date());
        int i = iprd.addPurchaseReturn(purchaseReturn);
        if (i > 0) {
            return Result.success("添加成功");
        } else {
            return Result.failure(400, "添加失败");
        }
    }

    @Override
    public Result<PurchaseReturn> queryPurchaseReturnById(String purchase_return_id) {
        PurchaseReturn purchaseReturn = iprd.queryPurchaseReturnById(purchase_return_id);
        if (purchaseReturn != null) {
            return Result.success(purchaseReturn);
        } else {
            return Result.failure(400, "暂无数据");
        }
    }

    @Override
    public Result<String> updatePurchaseReturnById(PurchaseReturn purchaseReturn) {
        int i = iprd.updatePurchaseReturnById(purchaseReturn);
        if (i > 0) {
            return Result.success("修改成功");
        } else {
            return Result.failure(400, "修改失败");
        }
    }

    @Override
    public Result<String> deletePurchaseReturn(String purchase_return_id) {
        int i = iprd.deletePurchaseReturn(purchase_return_id);
        if (i > 0) {
            return Result.success("删除成功");
        } else {
            return Result.failure(400, "删除失败");
        }
    }

    @Override
    public Result<String> updatePurchaseReturnAgree(String purchase_return_id) {
        //判断对应ID的订单状态是否为1与2，如果是,则返回当前订单状态为1——审核通过，2——审核拒绝，如果不是则将订单状态改为1
        System.out.println(purchase_return_id);
        int i = iprd.updatePurchaseReturnAgree(purchase_return_id);
        if (i > 0) {
            return Result.success(purchase_return_id + "审核通过");
        } else {
            return Result.failure(400, "审核状态为" + 1 + "，请勿重复审核");
        }
    }

    @Override
    public Result<String> updatePurchaseReturnRefuse(String purchase_return_id) {
        //判断对应ID的订单状态是否为1与2，如果是,则返回当前订单状态为1——审核通过，2——审核拒绝，如果不是则将订单状态改为1
        System.out.println(purchase_return_id);
        int i = iprd.updatePurchaseReturnRefuse(purchase_return_id);
        if (i > 0) {
            return Result.success(purchase_return_id + "审核拒绝");
        } else {
            return Result.failure(400, "审核状态为" + 2 + "，请勿重复审核");
        }
    }
}
